package org.csu.onlineedubackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.csu.onlineedubackend.entity.ForumPost;
import org.csu.onlineedubackend.mapper.ForumPostMapper;
import org.csu.onlineedubackend.service.ForumPostService;
import org.csu.onlineedubackend.service.ForumUserStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ForumPostServiceImpl implements ForumPostService {
    @Autowired
    private ForumPostMapper forumPostMapper;
    
    @Autowired
    private ForumUserStatsService forumUserStatsService;

    @Override
    public boolean createPost(ForumPost post) {
        // 發佈新帖子
        post.setStatus("active");
        boolean success = forumPostMapper.insert(post) > 0;
        
        // 更新用戶統計
        if (success) {
            forumUserStatsService.incrementPostCount(post.getUserId());
        }
        
        return success;
    }

    @Override
    public boolean updatePost(ForumPost post) {
        // 編輯帖子（只允許標題、內容、圖片修改）
        return forumPostMapper.updateById(post) > 0;
    }

    @Override
    public boolean deletePost(Integer postId, Integer userId) {
        // 刪除帖子（邏輯刪除，僅限本人）
        ForumPost post = forumPostMapper.selectById(postId);
        if (post == null || !post.getUserId().equals(userId)) {
            return false;
        }
        post.setStatus("deleted");
        boolean success = forumPostMapper.updateById(post) > 0;
        
        // 更新用戶統計
        if (success) {
            forumUserStatsService.decrementPostCount(userId);
        }
        
        return success;
    }

    @Override
    public IPage<ForumPost> getPostPage(Page<ForumPost> page, Integer tagId, Integer userId, String keyword) {
        // 分頁查詢帖子列表，支持標籤、用戶、關鍵字
        LambdaQueryWrapper<ForumPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ForumPost::getStatus, "active");
        if (userId != null) {
            wrapper.eq(ForumPost::getUserId, userId);
        }
        if (keyword != null && !keyword.isEmpty()) {
            wrapper.and(w -> w.like(ForumPost::getTitle, keyword).or().like(ForumPost::getContent, keyword));
        }
        // tagId 需要根據實際表設計進行關聯查詢，這裡暫略
        wrapper.orderByDesc(ForumPost::getCreatedAt);
        return forumPostMapper.selectPage(page, wrapper);
    }

    @Override
    public ForumPost getPostDetail(Integer postId) {
        // 查詢單個帖子詳情
        return forumPostMapper.selectById(postId);
    }

    @Override
    public List<ForumPost> getPostsByUser(Integer userId) {
        // 根據用戶查詢帖子
        LambdaQueryWrapper<ForumPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ForumPost::getUserId, userId).eq(ForumPost::getStatus, "active");
        wrapper.orderByDesc(ForumPost::getCreatedAt);
        return forumPostMapper.selectList(wrapper);
    }

    @Override
    public int getUserPostCount(Integer userId) {
        // 查詢用戶的發帖總數（只統計狀態為active的帖子）
        LambdaQueryWrapper<ForumPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ForumPost::getUserId, userId)
               .eq(ForumPost::getStatus, "active");
        return forumPostMapper.selectCount(wrapper).intValue();
    }
} 